﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.IO;
using System.Text;
using System.Diagnostics;
namespace Skyper.Shared {
	[DataContract(Name="Contact")]
	public sealed class ContactModel : BaseModel {
		#region Fields
		private static readonly Type MyType = typeof(ContactModel);
		#endregion
		#region Protected
		protected sealed override DataContractSerializer CreateSerializer() {
			return new DataContractSerializer(MyType,new Type[]{
				typeof(ContactModel[]),typeof(ContactStatus),
			});
		}
		#endregion
		#region Public
		public sealed override string ToString() {
			if (!String.IsNullOrEmpty(this.Displayname)) {
				return this.Displayname;
			}
			return base.ToString();
		}
		public sealed override bool Suspend(Stream stream) {
			//Check if stream is ok.
			if (stream == null || !stream.CanWrite) {
				return false;
			}

			var ds = this.Serializer;

			try {
				ds.WriteObject(stream, this);

				return true;
			} catch (Exception exc) {
				Debug.WriteLine(exc);

				return false;
			}
		}
		public sealed override bool Resume(Stream stream) {
			//Check if stream is ok.
			if (stream == null || !stream.CanRead) {
				return false;
			}

			var ds = this.Serializer;
			try {
				var me = ds.ReadObject(stream) as ContactModel;
				if (me == null) {
					return false;
				}

				this.Birthday = me.Birthday;
				this.City = me.City;
				this.Country = me.Country;
				this.Displayname = me.Displayname;
				this.Fullname = me.Fullname;
				this.Mood = me.Mood;

				return true;
			} catch (Exception exc) {
				Debug.WriteLine(exc);

				return false;
			}
		}
		#endregion
		#region Static
		public static bool Contains(IEnumerable<ContactModel> me, String username) {
			foreach (var mod in me) {
				if (mod.Username == username) {
					return true;
				}
			}
			return false;
		}
		public static bool Suspend(Stream stream, params ContactModel[] enumerator) {
			if (stream == null || !stream.CanWrite) {
				return false;
			}
			if (enumerator == null) {
				return false;
			}
			var ds = GetSerializer();

			try {
				ds.WriteObject(stream, enumerator);
				return true;
			} catch (Exception exc) {
				Debug.WriteLine(exc);
				return false;
			}
		}
		public static bool Resume(Stream stream, IList<ContactModel> list) {
			if (stream == null || !stream.CanRead) {
				return false;
			}
			ContactModel m = new ContactModel();

			var ds = GetSerializer();

			try {

				ContactModel[] data = ds.ReadObject(stream) as ContactModel[];

				if (data == null || data.Length == 0) {
					return false;
				}
				foreach (var c in data) {
					list.Add(c);
				}
				return true;
			} catch (Exception exc) {
				Debug.WriteLine(exc);
				return false;
			}
		}
		#endregion
		#region Property
		public Boolean Available {
			get {
				return (this.Status != ContactStatus.Unknown && this.Status != ContactStatus.SkypeOut && this.Status != ContactStatus.Offline);
			}
		}
		[DataMember(EmitDefaultValue=false,IsRequired=false)]
		public String Username {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public String Fullname {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public String Displayname {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public DateTime Birthday {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public String City {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public String Country {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public String Mood {
			get;
			set;
		}
		[DataMember(EmitDefaultValue = false, IsRequired = false)]
		public ContactStatus Status {
			get;
			set;
		}
		public String FriendlyName {
			get {
				if (!String.IsNullOrEmpty(this.Displayname)) {
					return this.Displayname;
				}
				if (!String.IsNullOrEmpty(this.Fullname)) {
					return this.Fullname;
				}
				return this.Username;
			}
		}
		#endregion
	}
}
